<?php
namespace Orm\UserRank;

class UserRankModel extends \Base\BaseOrmModel{

    function __construct()
    {
        parent::__construct();
        $this->table = 't_user_rank';
    }

    public function getRankStorageList($where, $offset, $size){
        $re = array();
        $sql = "SELECT
                    a.*, b.id as pid,b.`name`,
                    b.`portrait`,
                    c.`province`,
                    c.`city`,
                    c.`area`,
                    c.`address`,
                    c.`ship_username`,
                    c.`ship_mobile`
                FROM
                    " . $this->table . " a
                LEFT JOIN t_product b ON a.product_id = b.id
                LEFT JOIN t_user_address c ON  a.user_id = c.uid 
                WHERE
                    " . $where . "
                ORDER BY
                    a.`user_sort` ASC";
        $re['total'] = $this->_fetch($sql, 2);
        $sql .= " LIMIT " . $offset . "," . $size;
        $re['list'] = $this->_fetch($sql, 1);
        return $re;
    }

    /**
     * @param $where
     * @return array
     */
    public function getUserRankByWhere($where){
        $sql = "SELECT * FROM ".$this->table." WHERE `is_del` = 0 AND ".$where;
        return $this->_fetch($sql,1);
    }

    public function updateByUid($user_id){
        $sql = "UPDATE ".$this->table." SET `read` = 1 WHERE user_id={$user_id} AND type = 2";
        $this->execute($sql);
    }
}